//
//  HCMineAsksView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/18.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCMineAsksView.h"

#import "HCMineAsksTableViewCell.h"

#import "HCMineAskedModel.h"

@interface HCMineAsksView () <UITableViewDelegate,HCMineAsksTableViewCellSureClicked,UITableViewDataSource> {
    UITableView *tableView_;
    NSInteger model_;
    NSMutableArray *indexPaths;
    HCMineAskedModel *model_data;
}

@end

@implementation HCMineAsksView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubViews:frame];
    }
    return self;
}

-(void)initializeSubViews:(CGRect)frame {
    model_ = 0;
    indexPaths = [NSMutableArray array];
    self.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, frame.size.width-20, frame.size.height-10)];
    tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView_.delegate = self;
    tableView_.dataSource = self;
    tableView_.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    tableView_.showsVerticalScrollIndicator = NO;
    [self addSubview:tableView_];
    
    [tableView_ registerNib:[UINib nibWithNibName:@"HCMineAsksTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"askscell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (model_data) {
        return model_data.myask.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HCMineAsksTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"askscell" forIndexPath:indexPath];
    cell.model = 0;
    cell.delegate = self;
    if (model_data) {
        HCMineAskedData *data = model_data.myask[indexPath.row];
        cell.cellModel = data;
    }
    
    [cell.reloadButton addTarget:self action:@selector(cellBtnAction:event:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

-(void)cellBtnAction:(UIButton *)btn event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    
    CGPoint currentTouchPosition = [touch locationInView:tableView_];
    NSIndexPath *indexPath = [tableView_ indexPathForRowAtPoint:currentTouchPosition];
    
    if ([indexPaths containsObject:indexPath]) {
        [indexPaths removeObject:indexPath];
        HCMineAsksTableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];
        cell.model = 0;
        model_ = cell.model;
        cell.bgcontain.constant = 10;
        [cell.tableView_bottom reloadData];
        [tableView_ reloadData];
    }else{
        [indexPaths addObject:indexPath];
        HCMineAsksTableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];
        HCMineAskedData *answercc = model_data.myask[indexPath.row];
        cell.model = answercc.answer.count;
        model_ = cell.model;
        cell.bgcontain.constant = model_*60+10;
        [cell.tableView_bottom reloadData];
        [tableView_ reloadData];
    }
    
    NSLog(@"取得的数据:%ld",indexPaths.count);
    

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPaths containsObject:indexPath]) {
        return [HCMineAsksTableViewCell cellHeight:model_];
    }else{
        return 160;
    }
}

-(void)setModel:(id)model {
    model_data = (HCMineAskedModel *)model;
    
    if (model_data.path) {
        HCUSERDEFUALTS(model_data.path, @"headPath");
    }
    
    if (model_data.name) {
        HCUSERDEFUALTS(model_data.name, @"nameUser");
    }
    
    
    
    [tableView_ reloadData];
}

-(void)sureBtnClicked:(NSIndexPath *)indexPath cellEvent:(id)event{
    
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    
    CGPoint currentTouchPosition = [touch locationInView:tableView_];
    NSIndexPath *indexPath1 = [tableView_ indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"上一级的位置:%@---下一级的位置:%@",indexPath,indexPath1);
    
    if ([self.delegate respondsToSelector:@selector(answerUpload:lastIndexPath:)]&& self.delegate) {
        [self.delegate answerUpload:indexPath lastIndexPath:indexPath1];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
